package com.boxuegu.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.boxuegu.myapplication.adapter.ExercisesDetailAdapter;
import com.boxuegu.myapplication.bean.ExercisesDetailBean;
import com.boxuegu.myapplication.utils.AnalysisUtils;
import com.boxuegu.myapplication.view.TitleBar;

import java.io.InputStream;
import java.util.ArrayList;

public class ExercisesDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercises_detail);

        Intent intent=getIntent();//如果从另外的地方跳转到这里，本行获取跳转过来的意图
        int id=intent.getIntExtra("id",-1);
        String title="";
        if(id!=-1){
            title=intent.getStringExtra("title");
        }
        LinearLayout title_bar=findViewById(R.id.title_bar);
        TitleBar titleBar=new TitleBar(this,title+"习题");//标题栏的标题要与实际章节对应
        title_bar.addView(titleBar.getView());

        ListView lv_exercises_detail=findViewById(R.id.lv_exercises_detail);

        try {

            //生成数据源。1、读取文件p164:44
            InputStream inputStream = getResources().getAssets().open("chapter" + id + ".xml");
            //生成数据源。1、解析文件
            ArrayList<ExercisesDetailBean> beans = AnalysisUtils.getExercisesInfos(inputStream);

            //生成数据适配器对象
            ExercisesDetailAdapter adapter = new ExercisesDetailAdapter(this);
            adapter.setData(beans);

            lv_exercises_detail.setAdapter(adapter);

        }catch (Exception e){
            Toast.makeText(this,"题目文件打开或解析失败！",Toast.LENGTH_LONG).show();
        }


    }
}